package com.sino.operation.websocket;

import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
 * @Title: MyWebsocketServer
 * @Description:
 * @Author dengls
 * @date 2021/10/9 9:52
 */
@ServerEndpoint(value = "/myWebSocket/{param}")
@Component
@Log4j2
public class MyWebsocketServer {
    /**
     * 存放所有在线的客户端
     */
    private static Map<String, Session> clients = new ConcurrentHashMap<>();

    private static Map<String, String> clients2 = new ConcurrentHashMap<>();

    /**
     * @Author fangyu_yang
     * @Description 新建连接 并存入用户
     * @Date 10:33 2021/8/27
     * @Param [session]
     * @return void
     **/
    @OnOpen
    public void onOpen(@PathParam(value= "param" ) String param , Session session) {
        log.info("websocket建立连接开始-onOpen,用户id为："+param);
        //将新用户存入在线的组
        System.out.println("新用户连接："+session.getId());
        System.out.println("传入的用户id是："+param);
        clients.put(param, session);
        clients2.put(session.getId(),param);
        log.info("websocket建立连接结束-onOpen");
    }

    /**
     * @Author fangyu_yang
     * @Description 关闭客户端
     * @Date 10:34 2021/8/27
     * @Param [session]
     * @return void
     **/
    @OnClose
    public void onClose(Session session) {
        //将掉线的用户移除在线的组里
        String userID = clients2.get(session.getId());
        System.out.println(userID+"用户断开连接！");
        clients.remove(userID);
        clients2.remove(session.getId());
    }

    /**
     * @Author fangyu_yang
     * @Description 发生错误
     * @Date 10:38 2021/8/27
     * @Param [throwable]
     * @return void
     **/
    @OnError
    public void onError(Throwable throwable) {
        throwable.printStackTrace();
    }

    /**
     * @Author fangyu_yang
     * @Description 接受客户端消息
     * @Date 10:39 2021/8/27
     * @Param [message]
     * @return void
     **/
    @OnMessage
    public void onMessage(String message) {
        log.info("服务端收到客户端发来的消息: {}", message);

    }

    /**
     * @Author fangyu_yang
     * @Description 群发消息
     * @Date 10:39 2021/8/27
     * @Param [message]
     * @return void
     **/
    public static void sendAll(String message) {
        for (Map.Entry<String, Session> sessionEntry : clients.entrySet()) {
            sessionEntry.getValue().getAsyncRemote().sendText(message);
        }
    }


    /**
     * @Author fangyu_yang
     * @Description 发送任务给客户端
     * @Date 10:35 2021/8/27
     * @Param [object, userId]
     * @return void
     **/
    public static void sendTask(String task,String userId){
        Session session = clients.get(userId);
        session.getAsyncRemote().sendText(task);

    }

    /**
     * @Author fangyu_yang
     * @Description TODO 删除用户信息
     * @Date 16:02 2021/9/18
     * @Param [userId]
     * @return void
     **/
    public void deleteUser(String userId){
        Session session = clients.get(userId);
        clients2.remove(session.getId());
        clients.remove(userId);
    }
}
